package com.sinog.biz.schedule;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sinog.biz.mapper.havexml.other.LogInsertMapper;
import com.sinog.core.domain.InterfaceLog;
import com.sinog.core.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * @desc 处理http接口对接日志表数据
 * @author ypc
 * @date 2022/11/16
 */
@Slf4j
@Component
@ConditionalOnProperty(name = "gkzx.azbj.jkoulog.open",havingValue = "true")
@Transactional(rollbackFor = Exception.class)
public class InterfaceLogSchedule {

    @Resource
    private LogInsertMapper logInsertMapper;

    /**
     * 每天晚上11点执行处理http接口对接日志表数据
     */
    @Scheduled(cron = "${gkzx.azbj.jkoulog.cron}")
    public void updateTimeToDate() {
        log.info("=====开始处理http接口对接日志表中的time字段由时间戳类型转为日期格式=====");
        //获取List<实体类>
        QueryWrapper<InterfaceLog> wrapper = new QueryWrapper<>();
        List<InterfaceLog> interfaceLogList = logInsertMapper.selectList(wrapper);
        //循环拿出来 改变一下time值
        try {
            for (InterfaceLog interfaceLog : interfaceLogList) {
                //拿到字符串类型的毫秒级时间戳
                String time = interfaceLog.getTime();
                boolean flag = StringUtils.isNotEmpty(time) && !(StringUtils.contains(time,"-") || StringUtils.contains(time,":"));
                if (flag) {
                    Long timeLong = Long.valueOf(time);
                    String dateTime = DateUtil.chuoTransferDate(timeLong, "yyyy-MM-dd HH:mm:ss");
                    interfaceLog.setTime(dateTime);
                    //根据id修改这个查出来的实体类
                    logInsertMapper.updateById(interfaceLog);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            log.error("发生异常:" + e);
        }
        log.info("=====处理结束http接口对接日志表中的time字段由时间戳类型转为日期格式=====");
    }
}